package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.VersionEntity;

public class VersionDAO {
	public boolean insert(Connection conn, VersionEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"insert into version(title,content,version_code,version_name,add_admin_id,add_time,status,file_path,is_force,is_doc) values(?,?,?,?,?,?,?,?,?,?)");
		ps.setString(1, entity.getTitle());
		ps.setString(2, entity.getContent());
		ps.setInt(3, entity.getVersionCode());
		ps.setString(4, entity.getVersionName());
		ps.setInt(5, entity.getAddAdminId());
		ps.setTimestamp(6, entity.getAddTime());
		ps.setInt(7, entity.getStatus());
		ps.setString(8,entity.getFilePath());
		ps.setInt(9, entity.getIsForce());
		ps.setInt(10, entity.getIsDoc());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}
	
	public boolean update(Connection conn, VersionEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update version set title=?,content=?,version_code=?,version_name=?,add_admin_id=?,add_time=?,status=?,file_path=?,is_force=?,is_doc=? where id=?");
		ps.setString(1, entity.getTitle());
		ps.setString(2, entity.getContent());
		ps.setInt(3, entity.getVersionCode());
		ps.setString(4, entity.getVersionName());
		ps.setInt(5, entity.getAddAdminId());
		ps.setTimestamp(6, entity.getAddTime());
		ps.setInt(7, entity.getStatus());
		ps.setString(8,entity.getFilePath());
		ps.setInt(9, entity.getIsForce());
		ps.setInt(10, entity.getIsDoc());
		ps.setInt(11, entity.getId());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public VersionEntity findById(Connection conn, int id) throws SQLException {
		VersionEntity e = null;
		PreparedStatement ps = conn.prepareStatement("select * from version where id =? ");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			e = fill(rs);
		}
		return e;

	}

	private VersionEntity fill(ResultSet rs) throws SQLException {
		VersionEntity e = new VersionEntity();
		e.setId(rs.getInt("id"));
		e.setAddTime(rs.getTimestamp("add_time"));
		e.setContent(rs.getString("content"));
		e.setTitle(rs.getString("title"));
		e.setStatus(rs.getInt("status"));
		e.setVersionCode(rs.getInt("version_code"));
		e.setVersionName(rs.getString("version_name"));
		e.setAddAdminId(rs.getInt("add_admin_id"));
		e.setFilePath(rs.getString("file_path"));
		e.setIsForce(rs.getInt("is_force"));
		e.setIsDoc(rs.getInt("is_doc"));
		return e;
	}

	public List<VersionEntity> listDoc(Connection conn) throws SQLException {
		List<VersionEntity> list = new LinkedList<VersionEntity>();
		PreparedStatement ps = conn.prepareStatement("select * from version where is_doc=1 order by version_code desc");
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			list.add(fill(rs));
		}
		return list;
	}
	
	public List<VersionEntity> listPatient(Connection conn) throws SQLException {
		List<VersionEntity> list = new LinkedList<VersionEntity>();
		PreparedStatement ps = conn.prepareStatement("select * from version where is_doc=0 order by version_code desc");
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			list.add(fill(rs));
		}
		return list;
	}

}
